#!/usr/bin/env node

const Router = require('koa-router'),
	user = require('../models/user_mod'),
	//jwt = require('jsonwebtoken'),
	//{JWT_KEY} = require('../lib/constant.js'),
	//_ = require('lodash'),
	image = require('../models/image_mod'),
	router = new Router;
router.prefix('/api/user');


router.post('/register', async (ctx, next) => {
	let { user_phone, user_password, password_confim } = ctx.request.body;
	let res = await user.User_register(user_phone, user_password, password_confim);
	//if(res.code == 200){
	//	res.data = await jwt.sign(_.toPlainObject(res.data),JWT_KEY);
	//}
	ctx.body = res;
})
router.post('/slg', async (ctx, next) => {
	let { user_phone, user_password } = ctx.request.body;
	let res = await user.User_get_slg(user_phone, user_password);
	//if(res.code == 200){
	//       res.data = await jwt.sign(_.toPlainObject(res.data),JWT_KEY);
	//}
	ctx.body = res;
})
router.post('/getinfo', async (ctx, next) => {
	let { user_phone } = ctx.request.body;
	let res = await user.User_get_info(user_phone);
	ctx.body = res;
})
router.post('/getinfo2', async (ctx, next) => {
	let { user_id } = ctx.request.body;
	let res = await user.User_get_info2(user_id);
	ctx.body = res;
})

router.get('/', async (ctx, next) => {
	let res = await user.Look();
	ctx.body = res;
})
router.put('/', async (ctx, next) => {
	let file = ctx.request.files.File;
	let filename = '';
	if (file) {
		filename = await image.Image(file)
	}
	let { user_id, user_name, user_phone, user_email, user_password, password_confim, user_sign, password } = ctx.request.body;
	let res = await user.Modify(user_id, user_name, filename, user_phone, user_email, user_password, password_confim, user_sign, password);
	ctx.body = res;
})
router.delete('/', async (ctx, next) => {
	let { admin_id, admin_password, user_id, user_password } = ctx.request.body;
	let res = await user.DeleteUser(admin_id, admin_password, user_id, user_password);
	ctx.body = res;
})
module.exports = router;
